// pages/index/set_password/set_password.js
let network = require('../../../utils/requestManager.js');
let md5 = require('../../../utils/md5.min.js');
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    win_height: '',
    password: '',
    second_password: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      win_height: app.globalData.winHeight
    })
  },

  //获取账号
  bind_password: function (e) {
    console.log(e)
    let that = this;
    that.setData({
      password: e.detail.value
    })
  },

  //获取密码
  bind_second_password: function (e) {
    console.log(e)
    let that = this;
    let value = e.detail.value;
    if(value.length > 6){
      wx.showToast({
        title: '支付密码长度不能大于6',
        icon: 'none',
        duration: 2000
      });
      that.setData({
        second_password: value.slice(0, 6)
      })
    } else {
      that.setData({
        second_password: e.detail.value
      })
    }
  },

  //提交请求
  change_second_password: function () {
    let that = this;
    if (!that.data.password.length) {
      wx.showToast({
        title: '登录密码不能为空',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    if(!(that.data.second_password.length == 6)){
      wx.showToast({
        title: '支付密码长度必须为6',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    network.postRequest('backend/account/api/second_password/update', {
      password: md5(that.data.password),
      second_password: md5(that.data.second_password)
    }, function (res) {
      console.log(res);
      if (res.status == 0) {
        wx.navigateBack({
          delta: 1
        });
      } else {
        wx.showModal({
          title: '提示',
          content: res.detail,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})